package com.foreveredu.erlms.persist.hibernate;

import java.util.List;

import org.hibernate.Query;

import com.foreveredu.erlms.domain.Enroll;
import com.foreveredu.erlms.domain.PageModel;
import com.foreveredu.erlms.persist.IEnrollDao;

public class HibernateEnrollDao extends HibernateAbstractDao<Enroll, Integer>
		implements IEnrollDao {

	private static final long serialVersionUID = 1L;

	public HibernateEnrollDao() {
		super(Enroll.class);
	}
	
	@Override
	public PageModel findEnrollList(int offset ,int examOrder) {
		PageModel pm = new PageModel();
		Query query = this.getSession().createSQLQuery("select count(*) from examinee where examOrder = " + examOrder);
		Object count = query.uniqueResult();
		if(count != null){
			int total =Integer.parseInt(count.toString());
			pm.setTotal(total);
		}
		
		Query query2 = this.getSession().createQuery("from Enroll e where e.examinee.examOrder = ? order by e.id desc");
		query2.setInteger(0, examOrder);
		query2.setFirstResult(offset);
		if (offset + 10 > pm.getTotal()) {
			query2.setMaxResults(pm.getTotal()-offset);
		} else {
			query2.setMaxResults(10);
		}
		pm.setDatas(query2.list());
		
		return pm;
		
//		List<Enroll> enrolls = this.getHibernateTemplate().find("from Enroll e where e.examinee.examOrder = ? ",examOrder);
//		PageModel pm = new PageModel();
//		pm.setTotal(enrolls.size());
//		if (offset + 10 > enrolls.size()) {
//			pm.setDatas(enrolls.subList(offset, enrolls.size()));
//			return pm;
//		}
//		pm.setDatas(enrolls.subList(offset, offset + 10));
	}
	
	@Override
	public PageModel findEnrollListByPaid(int offset,int examOrder) {
		PageModel pm = new PageModel();
		Query query = this.getSession().createSQLQuery("select count(*) from examinee where examOrder = " + examOrder );
		Object count = query.uniqueResult();
		if(count != null){
			int total =Integer.parseInt(count.toString());
			pm.setTotal(total);
		}
		
		Query query2 = this.getSession().createQuery("from Enroll e where e.examinee.examOrder = ? and e.isPaid = 1 order by e.id desc");
		query2.setInteger(0, examOrder);
		query2.setFirstResult(offset);
		
		if (offset + 10 > pm.getTotal()) {
			query2.setMaxResults(pm.getTotal()-offset);
		} else {
			query2.setMaxResults(10);
		}
		pm.setDatas(query2.list());
		
//		List<Enroll> enrolls = this.getHibernateTemplate().find("from Enroll e where e.isPaid = 1 and e.examinee.examOrder = ?",examOrder);
//		PageModel pm = new PageModel();
//		pm.setTotal(enrolls.size());
//		if (offset + 10 > enrolls.size()) {
//			pm.setDatas(enrolls.subList(offset, enrolls.size()));
//			return pm;
//		}
//		pm.setDatas(enrolls.subList(offset, offset + 10));
		return pm;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public PageModel findEnrollListByIcon(int offset,int examOrder) {
		List<Enroll> enrolls = this.getHibernateTemplate().find("from Enroll e where e.icon is null and e.examinee.examOrder = ?",examOrder);
		PageModel pm = new PageModel();
		pm.setTotal(enrolls.size());
		if (offset + 10 > enrolls.size()) {
			pm.setDatas(enrolls.subList(offset, enrolls.size()));
			return pm;
		}
		pm.setDatas(enrolls.subList(offset, offset + 10));
		return pm;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Enroll> findEnrollByExamineeId(int id) {
		List<Enroll> enrolls = this.getHibernateTemplate().find("from Enroll e where e.examinee.id = ?",id);
		return enrolls;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public Enroll findEnrollByIdCardAndEnrollNumber(String idCard,
			String enrollNumber) {
		Enroll enroll = null;
		List<Enroll> enrollList = this.getHibernateTemplate().find(
						"from Enroll e where e.examinee.idCard = ? and e.examinee.enrollNumber = ?",
						new Object[] { idCard, enrollNumber });
		if (enrollList != null && enrollList.size() > 0) {
			enroll = enrollList.get(0);
		}
		return enroll;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Enroll> findEnrollsByExamOrder(int examOrder) {
		return this.getHibernateTemplate().find("from Enroll e where e.isPaid = 1 and e.examinee.enrollNumber is not null and e.examinee.examOrder = ?", examOrder);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Enroll> findNoPaidEnrollsByExamOrder(int examOrder) {
		return this.getHibernateTemplate().find("from Enroll e where e.isPaid = 0 and e.examinee.examOrder = ?", examOrder);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public Enroll findEnrollById(int id) {
		Enroll enroll = null;
		List<Enroll> enrollList = this.getHibernateTemplate().find("from Enroll e where e.id = ?",id);
		if (enrollList != null && enrollList.size() > 0) {
			enroll = enrollList.get(0);
		}
		return enroll;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Enroll> findEnrollByIdCard(String idCard, int examOrder) {
		List<Enroll> enrollList = this.getHibernateTemplate().find("from Enroll e where e.examinee.idCard = ? and e.examinee.examOrder = ? ",new Object[]{idCard, examOrder});
		return enrollList;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public Enroll findEnrollByIdCardAndSerialNumber(String idCard,
			String serialNumber, int examOrder) {
		Enroll enroll = null;
		List<Enroll> enrollList = this.getHibernateTemplate().find(
						"from Enroll e where e.examinee.idCard = ? and e.examinee.serialNumber = ? and e.examinee.examOrder = ?",
						new Object[] { idCard, serialNumber, examOrder });
		if (enrollList != null && enrollList.size() > 0) {
			enroll = enrollList.get(0);
		}
		return enroll;
	}

	@SuppressWarnings("unchecked")
	@Override
	public int findEnrollAllNumber(int examOrder) {
		int EnrollAllNumber = 0 ;
		List<Enroll> enrollList = this.getHibernateTemplate().find("from Enroll e where e.examinee.examOrder = ? ",examOrder);
		if (enrollList != null && enrollList.size() > 0) {
			EnrollAllNumber =enrollList.size();
		}
		return EnrollAllNumber;
	}

	@SuppressWarnings("unchecked")
	@Override
	public int findEnrollPaidNumber(int examOrder) {
		int EnrollPaidNumber = 0;
		List<Enroll> enrollList = this.getHibernateTemplate().find("from Enroll e where e.examinee.examOrder = ? and e.isPaid = 1 ",examOrder);
		if (enrollList != null && enrollList.size() > 0) {
			EnrollPaidNumber =enrollList.size();
		}
		return EnrollPaidNumber;
	}

	@SuppressWarnings("unchecked")
	@Override
	public int findEnrollKeepGradeNumber(int examOrder) {
		int EnrollKeepGradeNumber = 0 ;
		List<Enroll> enrollList = this.getHibernateTemplate().find("from Enroll e where e.examinee.examOrder = ? and e.keepGradeType != 0 ",examOrder);
		if (enrollList != null && enrollList.size() > 0) {
			EnrollKeepGradeNumber =enrollList.size();
		}
		return EnrollKeepGradeNumber;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public int findEnrollPaidNumberByToday(String now) {
		int EnrollPaidNumberByToday = 0;
		List<Enroll> enrollList = this.getHibernateTemplate().find("from Enroll e where e.paidTime = ? ",now);
		if (enrollList != null && enrollList.size() > 0) {
			EnrollPaidNumberByToday =enrollList.size();
		}
		return EnrollPaidNumberByToday;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public int findEnrollPaidNumberByTodayAndKeepGrade(String now) {
		int EnrollPaidNumberByTodayAndKeepGrade = 0;
		List<Enroll> enrollList = this.getHibernateTemplate().find("from Enroll e where e.paidTime = ? and e.keepGradeType != 0 ",now);
		if (enrollList != null && enrollList.size() > 0) {
			EnrollPaidNumberByTodayAndKeepGrade =enrollList.size();
		}
		return EnrollPaidNumberByTodayAndKeepGrade;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public Boolean findEnrollByDeptAndIdCard(int examOrder, String idCard,
			int rankId) {
		boolean i=false ;
		List<Enroll> enrollList = this.getHibernateTemplate().find("from Enroll e where e.examinee.idCard = ? and e.examinee.examOrder = ? and e.rank.id = ? ",new Object[]{idCard, examOrder,rankId});
		if(enrollList != null && enrollList.size() > 0){
			i = true ;
		}
		return i;
	}

}
